package com.example.demo01.interceptor;

import com.github.lianjiatech.retrofit.spring.boot.interceptor.BasePathMatchInterceptor;
import okhttp3.*;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * @ClassName BasePathMatchInterceptor
 * @Description
 * @Author zhangsw
 * @Date 2021/10/30 20:04
 * @Version 1.0
 */
@Component
public class DemoInterceptor extends BasePathMatchInterceptor {

    @Override
    protected Response doIntercept(Chain chain) throws IOException {
        Request request = chain.request();
        HttpUrl url = request.url();
        RequestBody body = request.body();
        boolean isHttps = request.isHttps();
        String method = request.method();
        Headers headers = request.headers();
        Object tag = request.tag();

        ThreadLocal<String> threadLocal = new ThreadLocal<>();
        String s = threadLocal.get();
        System.err.println(s);


        System.err.println("url：" + url);
        System.err.println("body：" + body);
        System.err.println("isHttps：" + isHttps);
        System.err.println("method：" + method);
        System.err.println("headers：" + headers);
        System.err.println("tag：" + tag);

        HttpUrl newUrl = url.newBuilder()
                .host("127.0.0.1")
                .port(8081)
//                .addEncodedPathSegment("demo/hello")
//                .encodedPath("demo/hello")

                .build();

        Request newRequest = request.newBuilder()
                .url(newUrl)
                .build();

        return chain.proceed(newRequest);
    }

    public static void main(String[] args) throws URISyntaxException {
        URI uri = new URI("http://10.10.21.81:15486/predict/spark/sparkcode15/0");
        System.err.println("uri.getHost() =>" + uri.getHost());
        System.err.println("uri.getPort() =>" + uri.getPort());
        System.err.println("uri.getPath() =>" + uri.getPath());

        String[] split = uri.getPath().split("/");
        for (String url : split) {
            System.err.println(url);
        }


    }
}
